package info.xingxingdd.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import info.xingxingdd.admin.common.PageQuery;
import info.xingxingdd.admin.common.Result;
import info.xingxingdd.admin.entity.ClientAppCategory;
import info.xingxingdd.admin.service.ClientAppCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 应用类别控制器
 */
@RestController
@RequestMapping("/api/app/category")
public class ClientAppCategoryController {
    
    @Autowired
    private ClientAppCategoryService clientAppCategoryService;
    
    /**
     * 分页查询
     */
    @PostMapping("/page")
    public Result<IPage<ClientAppCategory>> page(@RequestBody PageQuery query) {
        LambdaQueryWrapper<ClientAppCategory> wrapper = new LambdaQueryWrapper<>();
        
        // 关键字查询
        if (StringUtils.hasText(query.getKeyword())) {
            wrapper.like(ClientAppCategory::getCategoryName, query.getKeyword())
                   .or()
                   .like(ClientAppCategory::getCategoryKey, query.getKeyword());
        }
        
        // 按展示顺序排序
        wrapper.orderByAsc(ClientAppCategory::getIndex);
        
        Page<ClientAppCategory> page = new Page<>(query.getCurrent(), query.getSize());
        IPage<ClientAppCategory> result = clientAppCategoryService.page(page, wrapper);
        
        return Result.success(result);
    }
    
    /**
     * 查询所有
     */
    @GetMapping("/list")
    public Result<List<ClientAppCategory>> list() {
        LambdaQueryWrapper<ClientAppCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(ClientAppCategory::getIndex);
        List<ClientAppCategory> list = clientAppCategoryService.list(wrapper);
        return Result.success(list);
    }
    
    /**
     * 根据ID查询
     */
    @GetMapping("/{id}")
    public Result<ClientAppCategory> getById(@PathVariable Integer id) {
        ClientAppCategory category = clientAppCategoryService.getById(id);
        return Result.success(category);
    }
    
    /**
     * 新增
     */
    @PostMapping
    public Result<ClientAppCategory> save(@RequestBody ClientAppCategory category) {
        // 设置创建时间和更新时间
        LocalDateTime now = LocalDateTime.now();
        category.setCreateAt(now);
        category.setUpdateAt(now);
        
        clientAppCategoryService.save(category);
        return Result.success(category);
    }
    
    /**
     * 修改
     */
    @PutMapping
    public Result<ClientAppCategory> update(@RequestBody ClientAppCategory category) {
        // 设置更新时间
        category.setUpdateAt(LocalDateTime.now());
        
        clientAppCategoryService.updateById(category);
        return Result.success(category);
    }
    
    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Integer id) {
        clientAppCategoryService.removeById(id);
        return Result.success();
    }
} 